package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.RcglDailyReportConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglDailyReportCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglDailyReportQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglDailyReportResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglDailyReportUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglDailyReportEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglDailyReportMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglDailyReportService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 日常报告
 *
 * @author wenjie
 * @date 2024-04-26 14:42:29
 */
@Service
public class RcglDailyReportServiceImpl extends ServiceImpl<RcglDailyReportMapper, RcglDailyReportEntity> implements RcglDailyReportService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<RcglDailyReportResultDTO> getPageList(Params pageable, RcglDailyReportQueryDTO dto) {
        return Pages.convert(this.baseMapper.pageList(Pages.page(pageable), dto));
    }

    @Override
    public RcglDailyReportResultDTO getById(Long id) {
        RcglDailyReportResultDTO result = RcglDailyReportConvert.INSTANCE.convert(super.getById(id));
        this.setInstrumentList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(RcglDailyReportCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        RcglDailyReportEntity entity = RcglDailyReportConvert.INSTANCE.create(dto, dx.getTenantId());
        super.save(entity);
        //保存文件信息
        this.saveInstrumentList(dto, entity, user);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(RcglDailyReportUpdateDTO dto) {
        RcglDailyReportEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        super.updateById(RcglDailyReportConvert.INSTANCE.update(dto));
        //保存文件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    public void saveInstrumentList(RcglDailyReportCreateDTO dto, RcglDailyReportEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_DAILY_REPORT_INSTRUMENT), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(RcglDailyReportUpdateDTO dto, RcglDailyReportEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_DAILY_REPORT_INSTRUMENT),
                dto.getInstrumentList(),
                user);
    }

    private void setInstrumentList(RcglDailyReportResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_DAILY_REPORT_INSTRUMENT);
        dto.setInstrumentList(fileService.findList(query));
    }
}